import rss from './sys.config.js'
import store from "../store";

/**
 * 定义所有要调用的接口
 */
const API = {
    // 查询 套餐列表信息
    svipList() {
        return rss.get("/svip/list", {auth: true});
    },
    generatorOrder(confId) {
        return rss.post("/alipay/generator/order/" + confId, {}, {auth: true});
    },
    payUnFinishOrder(ordernum) { // 继续支付
        return rss.post("/alipay/pay/" + ordernum, {}, {auth: true});
    },
    alipayCallback(params) {
        return rss.get("/alipay/callback", {params}, {auth: true});
    },
    checkOrder(ordernum) {
        return rss.put("/alipay/pay/check/" + ordernum, {}, {auth: true});
    },
    cancelOrder(orderId) {
        return rss.delete("/order/" + orderId, {auth: true});
    },
    registOne({tel, password, email, uid, type, sign}) {
        return rss.post("/register", {tel, password, email, uid, type, sign});
    },
    registTwo({avatar, nickName, realName, birth, sex, uid, sign}) {
        return rss.put("/register/" + uid, {avatar, nickName, realName, birth, sex, userId: uid, sign});
    },
    registThree({uid, step, sign}) {
        return rss.put("/register", {userId: uid, step, sign});
    },
    uploadAvatar(avatar) {
        return rss.postForm("/upload", {avatar});
    },
    preview(path) {
        //if (path == null || path == "") return new URL("../assets/images/unknown.png", import.meta.url).href;
        return rss.defaults.baseURL + "/preview?path=" + path + "&type=inline";
    },
    loginTel(username, password) {
        return rss.post("/api/token", {username, password}, {
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            }
        });
    },
    refresh(refreshToken) {
        return rss.put("/api/token", {refresh: refreshToken}, {
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            }
        })
    },
    getEmailCode(email, subject) {
        return rss.post("/email/code", {email, subject}, {
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            }
        })
    },
    emailLogin(email, code) {
        return rss.put("/login", {email, code}, {
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            }
        })
    },
    queryOrderList(page, rows) {
        return rss.get("/order/list", {params: {page, rows}, auth: true});
    },
    captchaCodeUrl() {
        return rss.defaults.baseURL + "/captcha/code?w=140&h=40&u=" + btoa(store.getters.currentUser);
    },
    uploadResource(file) {
        return rss.postForm("/res/upload", {resource: file}, {auth: true});
    },
    queryResourceTypeList() {
        return rss.get("/res/types");
    },
    saveResource({name: resourceName, size, ext, url, keywords, resourceDesc, score, typeId, code: captchaCode}) {
        return rss.post("/res/add", {
            resourceName,
            size,
            ext,
            url,
            keywords: keywords.join("::"),
            resourceDesc,
            score,
            typeId,
            state: 0,
            captchaCode
        }, {auth: true})
    },
    querySysIndex() {
        // 该请求可登录查询
        return rss.get("/rss/index", {auth: true});
    },
    authorizeUrl(state) {
        return rss.defaults.baseURL + "/alipay/login?state=" + state;
    },
    authorizeCodeToToken(query) {
        // 发送请求到服务端
        return rss.post("/alipay/codeToToken", {...query});
    },

    bindAccount({uid, type, sign, tel, password}) {
        return rss.put("/bind/account", {uid, type, sign, tel, password}, {
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            }
        })
    },
    queryResourceDetail(resId) {
        return rss.get("/res/detail/" + resId, {auth: true});
    },
    queryCommentList({page, rows, resId}) {
        return rss.get("/res/comments/" + resId, {params: {page, rows}, auth: true});
    },

    queryResourcesBySearch(page, rows, search) {
        return rss.get("/res/q", {
            params: {
                page, rows, wd: search
            }
        })
    },
    queryResourcesByPage(page, rows, typeId) {
        return rss.get("/res/" + typeId, {params: {page, rows}});
    },
    queryUnReadMessages() {
        return rss.get("/notify/unReadMessage", {auth: true});
    },

    notifyUrl(uid) {
        return rss.defaults.baseURL + "/notify/subscribe/" + uid;
    },

    queryNotifyList(page, rows) {
        return rss.get("/notify/list", {params: {page, rows}, auth: true});
    },
    attentionUser(userId) {
        return rss.put("/user/attention", {to: userId}, {
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            auth: true
        })
    },
    modifyUserField({avatar, nickName, sex, oldEmail, newEmail}) {
        let contentType = avatar == null ? "application/x-www-form-urlencoded" : "multipart/form-data";
        return rss.put("/user/modify", {avatar, nickName, sex, oldEmail, newEmail}, {
            auth: true,
            headers: {
                "Content-Type": contentType
            },
        })
    },
    queryUserScores(page, rows) {
        return rss.get("/user/score/list", {params: {page, rows}, auth: true});
    },
    queryStaticsScoresShape() {
        return rss.get("/user/score/shape", {auth: true});
    },
    queryFriends() {
        return rss.get("/user/friends", {auth: true});
    },
    queryMessageRecords(offset, unread, toUser) {
        return rss.get("/message/records", {auth: true, params: {offset, unread, uid: toUser}})
    },
    chatWebSocketUrl(uid) { // 在线聊天地址
        return rss.defaults.baseURL.replace(/^https?/, "ws") + "/chat/" + uid;
    },
    updateMessageState(messageId) {
        return rss.put("/message/update/" + messageId, {}, {auth: true});
    },
    queryUserChatMessage(toUserId) {
        return rss.get("/user/friend/" + toUserId, {auth: true});
    },
    queryBlackListByPage(page, rows) {
        return rss.get("/message/black/list", {params: {page, rows}, auth: true});
    },
    bindBlackUser(blackUser) {
        return rss.post("/message/black/" + blackUser, {}, {auth: true});
    },
    unbindBlackUser(id) {
        return rss.delete("/message/black/" + id, {auth: true});
    },
    modifyPassword(oldPassword, newPassword) {
        return rss.put("/user/modify", {oldPassword, newPassword}, {
            auth: true,
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            }
        })
    },
    findPassword({email, code, password}) {
        return rss.put("/fwd", {email, code, password});
    },
    collectResource(resId) {
        return rss.post("/res/collect/" + resId, {}, {auth: true});
    },
    queryAdvertRank() {
        return rss.get("/rss/advert");
    },
    queryCollectListPage(page, rows) {
        return rss.get("/collect/list", {auth: true, params: {page, rows}}) ;
    },
    queryResourceListByUid(page, rows, uid) {
        return rss.get("/res/more/" + uid, {params: {page, rows}}) ;
    },
    checkDownloadCondition(resId) {
        return rss.post("/res/checkPermission/" + resId, {}, {auth: true});
    },
    downloadResPath(path, params) {
        let stringParams = "path=" + path ;
        // 拼接所有的参数
        for(let key in params) {
            stringParams += `&${key}=${params[key]}`
        }
        return rss.defaults.baseURL + "/res/download?" + stringParams ;
    },
    saveResourceComments({resId, star, content}) {
        return rss.post("/comment", {resId, star, content}, {auth: true});
    },
    checkFriend(uid) {
        return rss.put("/user/check/friend/" + uid, {}, {auth: true}) ;
    }
}


export default API;

